package teleradiologia.repository;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;

import teleradiologia.domain.Ordering;
import teleradiologia.utils.HibernateUtil;
import teleradiologia.web.TechnicianController;

public class OrderingDaoImpl implements OrderingDao {

	private static final Log log = LogFactory
			.getLog(TechnicianController.class);

	@Override
	public long create(Ordering ordering) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		long id = (Long) session.save(ordering);

		tx.commit();
		session.close();

		return ++id;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Ordering> getAll() {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		List<Ordering> orderings = session.createQuery(
				"from Ordering ordering order by ordering.id asc").list();

		tx.commit();
		session.close();

		return orderings;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Ordering> getAllByTechnician(String nip) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		List<Ordering> orderings = session
				.createQuery(
						"from Ordering ordering where ordering.technician=:nip order by ordering.id asc")
				.setString("nip", nip).list();

		tx.commit();
		session.close();

		return orderings;
	}

	@Override
	public void delete(Ordering ordering) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.delete(ordering);

		tx.commit();
		session.close();
	}

	@Override
	public Ordering getById(long id) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Ordering ordering = (Ordering) session
				.get(Ordering.class, new Long(id));

		tx.commit();
		session.close();

		return ordering;
	}

	@Override
	public void update(Ordering ordering) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.update(ordering);

		tx.commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Ordering> getAllByDiagnosticlab(String krs) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		List<Ordering> orderings = session
				.createQuery(
						"from Ordering ordering where ordering.diagnosticlab=:krs order by ordering.id asc")
				.setString("krs", krs).list();

		tx.commit();
		session.close();

		return orderings;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Ordering> getAllByPatient(long idpatient) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		List<Ordering> orderings = session
				.createQuery(
						"from Ordering ordering where ordering.patient.idpatient=:id order by ordering.id asc")
				.setLong("id", idpatient).list();

		tx.commit();
		session.close();

		return orderings;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Ordering> getAllByDiagnosticlabToArchive(String krs) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.roll(Calendar.MONTH, false);

		List<Ordering> orderings = session
				.createQuery(
						"from Ordering ordering where ordering.diagnosticlab=:krs "
								+ " and ordering.status!= :status and "
								+ " ordering.orderdate< :date order by ordering.id asc")
				.setString("krs", krs).setString("status", "archiwalne")
				.setDate("date", cal.getTime()).list();

		tx.commit();
		session.close();

		return orderings;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Ordering> getAllByTechnicianToArchive(String nip) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.roll(Calendar.MONTH, false);

		List<Ordering> orderings = session
				.createQuery(
						"from Ordering ordering where ordering.technician=:nip "
								+ " and ordering.status!= :status and "
								+ " ordering.orderdate< :date order by ordering.id asc")
				.setString("nip", nip).setString("status", "archiwalne")
				.setDate("date", cal.getTime()).list();

		tx.commit();
		session.close();

		return orderings;
	}

}
